package com.hxl.olympic.activity;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import com.hxl.olympic.R;
import com.hxl.olympic.util.App;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseActivity extends AppCompatActivity {
    Activity context;//上下文
    NetworkReceiver netReceiver;//网络监听
    String TAG;//TAG
    App app;//Application全局变量
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    LoadingDialog dialog;
    Unbinder unbinder;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initAll();
        initialize();
        initView();
        initData();
    }

    /**
     * 为Activity初始化必要的数据
     */
    private void initAll() {
        context = this;
        dialog = WidgetUtils.getLoadingDialog(context)
                .setIconScale(0.4F)
                .setLoadingIcon(R.mipmap.ic_none)
                .setLoadingSpeed(8);
        TAG = "BaseActivity";
        app = (App) getApplication();
        sp = getSharedPreferences("sp", MODE_PRIVATE);
        editor = sp.edit();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
        initNotificationImmerse();
        networkStatusMonitoring();
        //绑定初始化ButterKnife
        unbinder = ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
    }

    /**
     * 设置沉浸式通知栏
     */
    private void initNotificationImmerse() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 网络状态监听
     */
    private void networkStatusMonitoring() {
        IntentFilter intentFile = new IntentFilter();
        intentFile.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        intentFile.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intentFile.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        netReceiver = new NetworkReceiver();
        registerReceiver(netReceiver, intentFile);
    }

    public abstract void onClick(View v);

    class NetworkReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = connectMgr.getActiveNetworkInfo();
            //有无连接
            if (info == null) {
                //确认对话框
                DialogLoader.getInstance().showConfirmDialog(context, "网络连接已断开( ºΔº )", "设置", (dialog, which) -> {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                }, "好的");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unregisterReceiver(netReceiver);// 注销广播
        } catch (Exception e) {
            e.printStackTrace();
        }
        unbinder.unbind();
    }

    /**
     * onCreate初始化
     */
    public abstract void initialize();

    /**
     * 初始化控件
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public abstract void initData();

    /**
     * 初始化设置布局资源地址
     */
    public abstract int getLayoutId();


}
